#ifndef CRGCAM_H_INCLUDED
#define CRGCAM_H_INCLUDED


#include "act_types.h"
#include "mhdef.h"
#include "ccycle.h"
#include "feedback.h"
#include "timer.h"


typedef struct _CAM_FMT_BITS CAM_FMT_BITS;
typedef union _CAM_FMT CAM_FMT;

typedef struct _CAM_DOWN_FMT CAM_DOWN_FMT;
typedef struct _CAM1_BITS CAM1_BITS;
typedef struct _CAM2_BITS CAM2_BITS;
typedef struct _CAM3_BITS CAM3_BITS;
typedef union _CAM1 CAM1;
typedef union _CAM2 CAM2;
typedef union _CAM3 CAM3;

typedef struct _CAM_DOWN_FMT_SS CAM_DOWN_FMT_SS;

struct _CAM_DOWN_FMT_SS
{
    uchar zed : 1;
    uchar fz : 1;
    uchar yed : 1;
    uchar zjz : 1;
    uchar dm : 1;
    uchar yjz : 1;
    uchar reserve : 2;
};

struct _CAM_FMT_BITS
{
    uchar zjz : 1;
    uchar dm : 1;
    uchar yjz : 1;
    uchar zed : 1;
    uchar fz : 1;
    uchar yed : 1;
    uchar reserve : 2;
};

union _CAM_FMT
{
    uchar all;
    CAM_FMT_BITS bits;
};

struct _CAM1_BITS
{
    uchar hzjz_l : 1;
    uchar hdm_l : 1;
    uchar hyjz_l : 1;
    uchar hzed_l : 1;
    uchar hyed_l : 1;
    uchar hfz_l : 1;
    uchar qfz_l : 1;
    uchar qzed_l : 1;
};

union _CAM1
{
    uchar all;
    CAM1_BITS bits;
};

struct _CAM2_BITS
{
    uchar hzjz_r : 1;
    uchar hdm_r : 1;
    uchar hyjz_r : 1;
    uchar hzed_r : 1;
    uchar hyed_r : 1;
    uchar hfz_r : 1;
    uchar qfz_r : 1;
    uchar qzed_r : 1;
};

union _CAM2
{
    uchar all;
    CAM2_BITS bits;
};

struct _CAM3_BITS
{
    uchar qyed_l : 1;
    uchar qzjz_l : 1;
    uchar qdm_l : 1;
    uchar qyjz_l : 1;
    uchar qyed_r : 1;
    uchar qzjz_r : 1;
    uchar qdm_r : 1;
    uchar qyjz_r : 1;
};

union _CAM3
{
    uchar all;
    CAM3_BITS bits;
};

struct _CAM_DOWN_FMT
{
    CAM1 cam1;
    CAM2 cam2;
    CAM3 cam3;
};



struct _CAM
{
    uchar state[8];         /* 0-3左机头三角电磁铁状态，前1，后1，前2，后2
                               4-7右机头三角电磁铁状态
                            */
    uchar group_state[8];
    uchar state_bk[8];      /* 备份状态 */
    short pos[4];
    short pos_bk[4];
    CCYCLE cycle;
    CCYCLE reset_cycle;
    CHECK_TIMER timer;
    BOOL reset_all;             /* 是否全部复位 */
    BOOL restore;               /* 是否恢复状态 */

    int (*exec)(MH_UDP *pudp, ActType type, uchar *state);
    int (*mexec)(int ctype, short *delta, short *find_zero);
    int (*mreset)(int ctype, short *find_zero);
    int (*exec_reset_state)(uchar *cur_state, uchar *reset_state, int size);
};

typedef struct _CAM CAM;

CAM *cam_new();
void cam_set_cycle(CAM *cam, CCYCLE *cycle, CCYCLE *reset_cycle);
int cam_exec(MH_UDP *pudp, CAM *cam, ActType type, int group, int num);
int cam_exec_cycle(MH_UDP *pudp, void *user_data);
int cam_restore(MH_UDP *pudp, void *user_data);
void cam_start_timer(CAM *cam, TIMEOUT_FUNC pfunc, BOOL now);
int cam_exec_timer(MH_UDP *pudp, CAM *cam);
void cam_stop_cycle(CAM *cam);
int cam_check_error(MH_UDP *pudp, void *user_data);

BOOL cam_reset_complete(CAM *cam);
BOOL cam_restore_complete(CAM *cam);

void cam_convert_state(CAM *cam_unsed, uchar *cam_state_in, uchar *cam_state_out,int dir, BOOL reset_cmd);
void cam_set_pos(CAM *cam, short *pos, int num);
void cam_set_state(CAM *cam, uchar *state, int size);
void cam_get_state(CAM *cam, uchar *state, int size);
int cam_encount_error(CAM *cam, FEEDBACK *fb, CCYCLE *cycle);

CCYCLE* cam_get_cycle(CAM *cam);
BOOL cam_need_restore(CAM *cam);
void cam_set_restore(CAM *cam, BOOL restore);

void cam_set_group_state(CAM *cam, uchar state, int group);
uchar cam_get_group_state(CAM *cam, int group);
int cam_exec_group(MH_UDP *pudp, CAM *cam, int group);

#endif // CRGCAM_H_INCLUDED
